#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/2/19 14:12
# @Author  : YueXia
# @File    : custom_exception.py
# @Description : 自定义异常
from typing import Any
from .http_base import HttpCode


class BaseAppException(Exception):
    """应用异常基类"""

    def __init__(
        self,
        exc: HttpCode = None,
        *args,
        code: int = 400,
        msg: str = "错误",
        status_code: int = 200,
        echo_exc: bool = False,
        **kwargs,
    ):
        super().__init__()
        # 判断是否自定义异常，是返回自定义的异常，否则返回其他异常状态
        if exc:
            code, msg = exc.code, exc.msg
        self._code = code
        self._message = msg
        self._status_code = status_code
        self.echo_exc = echo_exc
        self.args = args or []
        self.kwargs = kwargs or {}

    @property
    def code(self) -> int:
        return self._code

    @property
    def msg(self) -> str:
        return self._message

    @property
    def status_code(self) -> int:
        return self._status_code

    def __str__(self):
        return "{}: {}".format(self.code, self.msg)


class AuthException(Exception):
    """认证失败"""

    def __init__(self, msg: str = "认证已失效，请您重新登录"):
        self.msg = msg


class PermissionException(Exception):
    """
    权限异常
    """

    def __init__(self, data: Any = None, message: str = "你没有访问权限"):
        self.data = data
        self.message = message
